// permission.js
import { Trie } from '@/utils/trie';
import Mock from 'mockjs';// 查看导入的 Trie 是否为构造函数
const state = {
  routes: [],
  addRoutes: [],
  mockList: [],
  trie: new Trie()
};

const mutations = {
  SET_ROUTES: (state, routes) => {
    console.log(111);
  },
  SET_MOCKLIST(state, list) {
    state.mockList = list;
    // 插入数据到前缀树
    list.forEach((item) => {
      state.trie.insert(item.label, item);
    });
  }
};

const actions = {
  generateRoutes({ commit }, roles) {
    console.log(222);
  },
  generateMockList({ commit }) {
    const mockData = Mock.mock({
      'list|3000': [
        {
          'key|+1': 0,
          label: function () {
            return `${Mock.mock('@cname')}`;
          }
        }
      ]
    });
    commit('SET_MOCKLIST', mockData.list);
  }
};

const getters = {
  getMockList: (state) => state.mockList,
  searchMockList: (state) => (prefix) => state.trie.fuzzySearch(prefix)
};

console.log('getters:', getters);
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};
